package com.shop2cn.iapi.agentproducts.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.DeleteProductRebateListBean;
import com.shop2cn.iapi.agentproducts.parameter.EditProductRebateBean;
import com.shop2cn.iapi.agentproducts.service.DeleteProductRebateListCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agentrebate.Ymt_AgentRebate;
import com.ymttest.database.model.agentrebate.Ymt_AgentRebateExample;
/**
*删除商品分佣Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteProductRebateList{
	private static DeleteProductRebateListBean deleteproductrebatelistBean;
	private static DeleteProductRebateListCall deleteproductrebatelistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除商品分佣Test");
	}

	@Before
	public void caseUp() {
		deleteproductrebatelistBean=new DeleteProductRebateListBean();
		deleteproductrebatelistCall=new DeleteProductRebateListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deleteproductrebatelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteProductRebateList_001() {
		Logger.start(true,"正确数据，成功");
		try {
			//创建一个商户和分销小店
			Integer MchId = GodmchManageCallService.CreateGodMch();
			Integer StoreId = GodmchManageCallService.CreateStores(MchId, 20);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			EditProductRebateBean initEditProductRebateBean = AddAgentProductCallService.initEditProductRebateBean(productId,StoreId, MchId,  50);
			AddAgentProductCallService.EditProductRebateBean(initEditProductRebateBean);
			
			deleteproductrebatelistBean.setProductId(productId);
			deleteproductrebatelistBean.setUserId(MchId);
			deleteproductrebatelistCall.setData(deleteproductrebatelistBean);
			deleteproductrebatelistCall.callService();			
			Logger.verifyEquals("200", deleteproductrebatelistCall.getString("code"), "验证code");
			
			Ymt_AgentRebateExample example=new Ymt_AgentRebateExample();
			example.createCriteria().andProductIdEqualTo(productId);
			List<Ymt_AgentRebate> agnetrebates = SqlMapperFactory.getIntergratedProductMapper().selectYmtAgentRebateExample(example);
			Logger.verifyEquals(true,agnetrebates.size()>0,"验证查询不为空");
			agnetrebates.forEach(b->{
				Logger.verifyEquals(-1,b.getAction(), "已经删除，action为-1");
			});
			
			Thread.sleep(5000);
			//验证删除的时候发送总线消息
			MessageListBean MessageListBean2 = MqQueryCallService.initEditProductRebate(productId);
			 
			List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			Logger.debug(joa2);
			int redo = 0;
			while (joa2.size() < 2 && redo < 3) {
				Thread.sleep(5000);
				joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
				redo++;
			}
			String actionType = "UpdateAgentProductRebate";
			String appId = "agentproducts.iapi.ymatou.com";
			Logger.verifyEquals(actionType, joa2.get(0).get("ActionType"), "验证ActionType");
			Logger.verifyEquals(productId, joa2.get(0).get("ProductId"), "验证ProductId");
			Logger.verifyEquals(appId, joa2.get(0).get("AppId"), "验证AppId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}